<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button @click="needSendMessage()" type="primary">是否发送提醒</el-button> -->
        <el-button  type="primary" @click="downTemplate()">下载模板</el-button>
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="uploadSuccess">
          <el-button  type="primary">导入模板</el-button>
        </el-upload>
        <!-- <el-button v-if="isAuth(':edupreparestudent:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        width="80"
        label="序号">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        width="100"
        label="学员姓名">
      </el-table-column>
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        width="120"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="gender"
        header-align="center"
        align="center"
        label="性别">
        <template slot-scope="scope">
          <div v-if="scope.row.gender == 2">男</div>
          <div v-if="scope.row.gender == 1">女</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="birthday"
        header-align="center"
        align="center"
        width="160"
        label="生日">
      </el-table-column>
      <el-table-column
        prop="gradeName"
        header-align="center"
        align="center"
        width="100"
        label="年级">
      </el-table-column>
      <el-table-column
        prop="realSchoolName"
        header-align="center"
        align="center"
        width="160"
        label="就读学校">
      </el-table-column>
      <el-table-column
        prop="score"
        header-align="center"
        align="center"
        width="140"
        label="入班考试分数">
      </el-table-column>
      <el-table-column
        prop="gmtCreate"
        header-align="center"
        align="center"
        width="160"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="createUserName"
        header-align="center"
        align="center"
        label="创建者人">
      </el-table-column>
      <!-- <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          name: ''
        },
        batchNo: '',
        dataList: [],
        EduPrepareStudentEntity: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        downTemplateUrl: '',
        uploadUrl: this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {///sys/send/messages
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/edupreparestudent/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name,
            'batchNo': this.batchNo
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      getUploadInfo() {
        this.$http({
          url: this.$http.adornUrl('/sys/edupreparestudent/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 1000,
            'batchNo': this.batchNo
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.EduPrepareStudentEntity = data.page.list
          }
        })
      },
      //是否发送提醒
      needSendMessage() {
        this.$confirm(`导入成功，是否发送提醒给学生`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          this.sendMessage();
        })
      },  
      //导入成功后 发送短信 EduPrepareStudentEntity
      sendMessage() {
        this.$http({
          url: this.$http.adornUrl('/sys/send/messages'),
          method: 'post',
          data: this.$http.adornData({
            'EduPrepareStudentEntity': this.EduPrepareStudentEntity

          })
        }).then(res => {
          console.log(res)
          if(res.data.code == 0) {
            this.$message({
              message: '短信发送成功',
              type: 'success',
            })
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      //下载模板
      downTemplate() {
        this.$http({
          url: this.$http.adornUrl('/sys/oss/download/template'),
          method: 'post',
          
        }).then(({data})=> {
          if(data && data.code === 0) {
            this.downTemplateUrl = data.url
            window.location.href = this.downTemplateUrl;
            // window.location.href = 'https://zyfedu.oss-cn-beijing.aliyuncs.com/template/%E5%AD%A6%E5%91%98%E4%BF%A1%E6%81%AF%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xls?Expires=1624420886&OSSAccessKeyId=TMP.3KkMymnV5XqvU9by5j4ZumL5K3eeD4rszxTHkqHW8cFEgpQr2PzkoV9dxRhkrrE5sJ1cAjDYqasSR3MH12W8jKWhkKdVW3&Signature=Js4n4838gVV3Jy%2Bxen%2BiW4uxigg%3D&versionId=CAEQDxiBgMChkLDz0BciIDg5MTBmN2U4MDMyYjRhOGFhODNhMTRjNzI0YWU0NTNk&response-content-type=application%2Foctet-stream'
          }
        })
      },
      //导入模板成功
      uploadSuccess(res,file) {
        console.log(res);
        if(res.code == 0) {//batchNo
          this.$message({
            message: '导入成功',
            type: 'success',
          })
          this.batchNo = res.batchNo;
          this.getUploadInfo();
          this.pageIndex = 1;
          this.getDataList();
          this.needSendMessage();
         

        }else{
          this.$message.error(res.msg);
        }
      },
      // 删除
      // deleteHandle (id) {
      //   var ids = id ? [id] : this.dataListSelections.map(item => {
      //     return item.id
      //   })
      //   this.$confirm(`确定要进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     this.$http({
      //       url: this.$http.adornUrl('//edupreparestudent/delete'),
      //       method: 'post',
      //       data: this.$http.adornData(ids, false)
      //     }).then(({data}) => {
      //       if (data && data.code === 0) {
      //         this.$message({
      //           message: '操作成功',
      //           type: 'success',
      //           duration: 1500,
      //           onClose: () => {
      //             this.getDataList()
      //           }
      //         })
      //       } else {
      //         this.$message.error(data.msg)
      //       }
      //     })
      //   })
      // }
    }
  }
</script>
<style scoped>
.upload-demo{
  display: inline-block;
}
.el-upload-list {
  display: none!important;
  height: 0!important;
}
.el-upload-list__item{
  display: none!important;
  height: 0!important;
}
</style>
